<template>
  <div class="page login-contain">
    <div class="title-container">
      <h2>用户登录</h2>
      <el-form :model="form" :rules="rules" ref="formRef">
        <el-form-item class="input" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名">
            <template #prefix>
              <el-icon class="el-input__icon"><userFilled /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item class="input" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码">
            <template #prefix>
              <el-icon class="el-input__icon"><goodsFilled /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item>
          <div class="btn">
            <el-button @click="onSubmit">登录</el-button>
            <el-button @click="resetForm">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>


<script>
import { onMounted, reactive, ref } from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { api_login } from '../../service/testRequest'
import { routes } from  '../../router/index'
export default {
  setup() {
    const router = useRouter();
    const store = useStore();
    const input = ref("");
    const form = reactive({
      username: "",
      password: "",
    });
    const formRef = ref(null);
    const rules = {
      username: [
        {
          required: true,
          message: "请输入用户名",
          trigger: "blur",
        },
      ],
      password: [
        {
          required: true,
          message: "请输入密码",
          trigger: "blur",
        },
      ],
    };

  /*   const onSubmit = () => {
      formRef.value.validate((valid) => {
        if (valid) {
          console.log("登录成功!!");
          store.commit("setform", form); //登录成功，将数据存储到vuex和localstorage
          //跳转至默认主页
          let queryRedirectPath = "/";
          //如果redirect参数中带有值，则登录成功后跳转至对应的界面
          //let redirect = router.currentRoute.value.query.redirect;
          // if(redirect){
          // queryRedirectPath = redirect;
          // }
          router.push({ path: queryRedirectPath });
        } else {
          console.log("登录失败!!");
          return false;
        }
      });
    };
 */
    const onSubmit=async ()=>{
      const goLogin=async ()=>{
        const res = await api_login(form);
        console.log('res',res);
        if(res.data){
          localStorage.setItem('user',JSON.stringify(res.data.username));
          store.commit('user/doSetUser',res.data.user);
          let userType = res.data.user.userType;
          const enableRouteList = routes.filter(route=>route.meta.userType <= userType);
          localStorage.setItem('enableRouteList',JSON.stringify(enableRouteList));
          store.commit('routes/doSetRoute',enableRouteList)
          console.log('user',store.state.user);
          router.replace('/');
        }
      }
      formRef.value.validate((valid,field)=>{
        if(valid){
          goLogin();
        }else{
          Message.error('请正确填写表单');
        }
      });
    };
    onMounted(()=>{
      console.log("路由信息",routes)
    })
   
    const resetForm = () => {
      console.log("清空");
      formRef.value.resetFields();
    };
    return {
      input,
      form,
      rules,
      formRef,
      onSubmit,
      resetForm,
    };
  },
};
</script>

<style scoped>
.login-contain {
  /* width: 520px;
  height: 500px;
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -260px;
  margin-top: -260px; */
  /* width: 400px;
  height: 260px;
  padding: 20px;
  margin: 0 auto;
  margin-top: 180px;
  border-radius: 10px;

  position: relative; */
  
}

.login-contain .title-container {
  text-align: center;
  position: relative;
  width: 520px;
  max-width: 100%;
  padding: 160px 35px 0;
  margin: 0 auto;
  overflow: hidden;
}

/* .login-contain .input {
  margin-top: 20px;
} */

.el-input {
  height: 40px;
}
.el-form-item {
  margin-top: 30px;
}

.login-contain .btn {
  display: block;
  margin: 0 auto;
}

.el-button {
  margin-right: 20px;
}
</style>